package typoker.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Util {

	public static synchronized String[] readStringFromFile(String filename) {
		InputStream is = Util.class.getResourceAsStream(filename);

		if (is != null) {
			List<String> temp = new ArrayList<String>();

			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));

			String text = "";
			try {
				while ((text = reader.readLine()) != null) {
					temp.add(text);
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			return temp.toArray(new String[0]);
		}

		return null;
	}
	
	public static short[] readShortFromFile(String filename) {
		InputStream is = Util.class.getResourceAsStream(filename);

		if (is != null) {
			List<Short> temp = new ArrayList<Short>(512);
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			
			String text = "";
			try {
				while ((text = reader.readLine()) != null) {
					temp.add(Short.parseShort(text));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			short[] tempArray = new short[temp.size()];
			
			for (int i = 0; i < tempArray.length; i++) {
				tempArray[i] = temp.get(i);
			}
			
			return tempArray;
		}

		return null;
	}
}